package com.jphoebe.plugin.mybatis.dom.converter;

import com.intellij.psi.ElementManipulators;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiReference;
import com.intellij.psi.xml.XmlAttributeValue;
import com.intellij.util.xml.*;
import com.jphoebe.plugin.mybatis.reference.ResultPropertyReferenceSet;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author JPhoebe
 */
public class PropertyConverter extends AbstractConverterAdaptor<XmlAttributeValue> implements CustomReferenceConverter<XmlAttributeValue> {

  @NotNull
  @Override
  public PsiReference[] createReferences(GenericDomValue<XmlAttributeValue> value, PsiElement element, ConvertContext context) {
    final String s = value.getStringValue();
    if (s == null) {
      return PsiReference.EMPTY_ARRAY;
    }
    return new ResultPropertyReferenceSet(s, element, ElementManipulators.getOffsetInElement(element)).getPsiReferences();
  }

  @Nullable
  @Override
  public XmlAttributeValue fromString(@Nullable @NonNls String s, ConvertContext context) {
    DomElement ctxElement = context.getInvocationElement();
    return ctxElement instanceof GenericAttributeValue ? ((GenericAttributeValue)ctxElement).getXmlAttributeValue() : null;
  }

}
